package fr.umlv.m2.recipes.ideas;

import java.util.ArrayList;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import fr.umlv.m2.recipes.ideas.bean.RecipeBean;
import fr.umlv.m2.recipes.ideas.sqlite.db.objects.RecipesDB;

/**
 * Create the Menu Tab Widget use in the Home activity
 * @author Younes Chandarli et Christophe Chillan
 *
 */
public class MenuTabWidget extends TabActivity {
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.menutab);

	    Resources res = getResources(); // Resource object to get Drawables
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, RecipesWidget.class);
	    Bundle extras = this.getIntent().getExtras();
	    Bundle bundle;
	    if(extras != null && extras.getSerializable("recipes")!=null){
	    	bundle = extras;
	    }else{
		    final ArrayList<RecipeBean> recipes = new ArrayList<RecipeBean>(RecipesDB.getRecipeDao().getAllRecipes(RecipesDB.getDatabase()));		    
		    bundle = new Bundle();
			bundle.putSerializable("recipes",recipes);
	    }
		intent.putExtras(bundle);
		
	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec("liste des recettes").setIndicator("Recettes",
	                      res.getDrawable(R.drawable.list))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    // Do the same for the other tabs
	    intent = new Intent().setClass(this, SearchWidget.class);
	    spec = tabHost.newTabSpec("recherche").setIndicator("Recherche",
	                      res.getDrawable(R.drawable.search))
	                  .setContent(intent);
	    tabHost.addTab(spec);

	    intent = new Intent().setClass(this, FavoriteWidget.class);
	    spec = tabHost.newTabSpec("favoris").setIndicator("Favoris",
	                      res.getDrawable(R.drawable.favorite))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    
	    intent = new Intent().setClass(this, HelpWidget.class);
	    spec = tabHost.newTabSpec("aide").setIndicator("Aide",
	                      res.getDrawable(R.drawable.ic_tab_help))
	                  .setContent(intent);
	    tabHost.addTab(spec);
	    if(extras!=null && extras.getSerializable("indexTab")!=null){
	    	 tabHost.setCurrentTab(extras.getInt("indexTab"));
	    }else{
	    	tabHost.setCurrentTab(0);
	    }
	}
}
